/**
 * 
 */
package cn.gov.msa.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.gov.msa.model.Dictionary;
import cn.gov.msa.model.DictionaryDetail;
import cn.gov.msa.model.Role;
import cn.gov.msa.service.DictionaryDetailService;
import cn.gov.msa.service.DictionaryService;
import cn.gov.msa.service.RoleService;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.github.pagehelper.PageInfo;


/**
 * @author Administrator
 *
 */
//@Controller
@RestController
@RequestMapping(value = "/role", produces="text/plain;charset=UTF-8")
public class RoleController {
	ObjectMapper mapper = new ObjectMapper(); 	
	
	@Autowired
	private RoleService roleService;
	
	
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params) throws Exception {
		String result = "";
		
		ObjectMapper mapper = new ObjectMapper(); 	
		
		int pageNum = 1;
		int pageSize = 10;
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		

		Role role = mapper.readValue(params.toString(), Role.class);
		
		List<Role> roleList = roleService.selectRole(pageNum, pageSize, role);		

	    //获取分页信息
        PageInfo page = new PageInfo(roleList);
        

		JSONArray ja = JSONArray.fromObject(roleList);
		
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);
		
		
		result = jsonResult.toString();
		return result;
	}
	
	@RequestMapping(value = "/delete")
	public String delete(String rolecode) throws Exception{
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{
			JSONObject param = new JSONObject();
			param.put("rolecode", rolecode);
			japaram.add(param);			
			
			int delete = roleService.deleteRole(rolecode);			
			
			result = delete > 0 ? 1 : 0;
			info = delete > 0 ? InfoObjUtil.Success_delete_OK : InfoObjUtil.Error_delete_no;
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);
		
		
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/batchDelete")
	public String batchDelete(String rolecodes) throws Exception{
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{			
			String rolecodeResult = "";
			
			for(int i = 0 ; i < rolecodes.split(",").length; i++){	
				String rolecode = rolecodes.split(",")[i];
				int delete = roleService.deleteRole(rolecode);
				
				if(delete > 0)
					rolecodeResult += rolecode + ",";
			}
			
			rolecodeResult = rolecodeResult.substring(0, rolecodeResult.length() - 1);

			JSONObject param = new JSONObject();
			param.put("rolecode", rolecodeResult);
			japaram.add(param);
			
			result = rolecodeResult.split(",").length > 0 ? 1 : 0;
			info = rolecodeResult.split(",").length > 0 ? "批量删除成功！" : "批量删除失败！";
		}
		catch(Exception e){
			result = 2;
			info = "删除异常!";
			e.printStackTrace();
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);		
		
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/save")
	public String save(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false;
	    JSONObject jsonResult = new JSONObject(); 
		
	    try{
			//得到角色的model数据
			Role role = mapper.readValue(param.toString(), Role.class);	
		    
			Role r = roleService.getInfoByRoleCode(role.getRolecode());

			if(r == null){
			    save = roleService.saveData(role);
	
			    if(save){
			    	result = 1;
			    	info = InfoObjUtil.Success_save_OK;
			    }
			    else{
			    	result = 0;
			    	info = InfoObjUtil.Error_save_no;
			    }
			}
			else{
		    	result = 0;
		    	info = "角色代码不能重复！";
			}
	    }
	    catch(Exception e){
			e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }
	    
	    
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/update")
	public String update(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false; 
		
	    try{
			
			//得到类型的model数据
			Role role = mapper.readValue(param.toString(), Role.class);	
		    
		    save = roleService.updateData(role);
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }
	    
	    
	    JSONObject jsonResult = new JSONObject();
	    if(save){
	    	result = 1;
	    	info = InfoObjUtil.Success_save_OK;
	    }
	    else{
	    	result = 0;
	    	info = InfoObjUtil.Error_save_no;
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	


	@RequestMapping(value = "/getInfo")
	public String getInfo(String rolecode) throws Exception {
		int result = 0;
		String info = "";
		
		JSONObject joparam = new JSONObject();
		
		try{

			Role role = roleService.getInfoByRoleCode(rolecode);		
			
			result = role != null ? 1 : 0;
			info = role != null ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
			
			joparam = JSONObject.fromObject(role);
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Error_NOINTERFACE;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", joparam);
		
		System.err.println(jsonResult);

		return jsonResult.toString();
	}
	
	

	@RequestMapping(value = "/checkrepeat")
	public String checkrepeat(String rolecode) throws Exception {

		int result = 0;
		String info = "";
		
		
		try{
			Role role = roleService.getInfoByRoleCode(rolecode);

			result = role != null ? 1 : 0;
			info = role != null ? "角色已存在！" : InfoObjUtil.Error_NODATA;

		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Error_NOINTERFACE;
			e.printStackTrace();
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		
		System.err.println(jsonResult);

		return jsonResult.toString();
		
	}
}
